package com.hengtiansoft.lfy;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.util.Log;

import com.hengtiansoft.lfy.chart.DemoHXSDKHelper;
import com.hengtiansoft.lfy.chart.net.VolleyUtil;
import com.hengtiansoft.lfy.constants.Constants;
import com.hengtiansoft.lfy.constants.Constants;
import com.hengtiansoft.lfy.db.DatabaseHelper;
import com.hengtiansoft.lfy.chart.applib.utils.HXPreferenceUtils;
import com.hyphenate.EMCallBack;
import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.umeng.socialize.PlatformConfig;
import com.ztspeech.translator.ZTTranslator;
import com.umeng.socialize.PlatformConfig;

import cn.jpush.android.api.JPushInterface;

/**
 * 应用入口
 * daiye
 * 2016/10/24.
 */
public class LFYApplication extends Application {

    private static Context mContext;
    private static LFYApplication instance;
    public static String PACKAGE_NAME = "";
    //科大讯飞----------------------------
    public SpeechSynthesizer mTts;
    private String TAG="Kdxf_TTSPlayer";
    private static String SettingSexChoose = "Setting_Sex_Choose";
    private static String SettingSpeedChoose = "Setting_Speed_Choose";
    private static String AUTOPLAYER = "auto_player";
    private static String SettingMsgSpeaker="Setting_Msg_Speaker2";
    private static SharedPreferences mSharedPreferences;
    private static SharedPreferences.Editor editor;
    public static final String PREFERENCE_NAME = "lfy";
    public static DatabaseHelper dbinstance;
    public static DemoHXSDKHelper hxSDKHelper = new DemoHXSDKHelper();
    /**
     * 单例，返回一个实例
     * @return
     */
    public static LFYApplication getInstance() {
        if (instance == null) {
//            LogUtil.w("[ECApplication] instance is null.");
        }
        return instance;
    }

    public static Context getContext() {
        return mContext;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        mContext = getApplicationContext();
        hxSDKHelper.onInit(instance);
        PACKAGE_NAME = getPackageName();
        mSharedPreferences = getSharedPreferences(PREFERENCE_NAME,
                Context.MODE_PRIVATE);
        editor = mSharedPreferences.edit();
        // 实例化volley
        VolleyUtil.init(this);
        addPlatfotm();
        initImageLoader();
        HXPreferenceUtils.init(this);
        //科大讯飞初始化
        SpeechUtility.createUtility(LFYApplication.this, "appid=" + Constants.MSC_APP_ID);
        // 初始化合成对象
        mTts = SpeechSynthesizer.createSynthesizer(this, mInitListener);
        //设置翻译服务器地址
        ZTTranslator.getTranslatorInstance().setServerAddr(Constants.TRANSLATE);
        // jpush推送
        JPushInterface.init(this);
    }

    private void addPlatfotm(){
        PlatformConfig.setWeixin(Constants.WEIXIN_APP_ID, Constants.WEIXIN_APP_SECRET);
        //微信 appid appsecret
        PlatformConfig.setSinaWeibo(Constants.SINA_APP_ID, Constants.SINA_APP_KEY);
        //新浪微博 appkey appsecret
        PlatformConfig.setQQZone(Constants.QQ_APP_ID, Constants.QQ_APP_KEY);
        // QQ和Qzone appid appkey
    }

    /**
     * 初始化监听器。
     */
    private InitListener mInitListener = new InitListener() {

        @Override
        public void onInit(int code) {
            Log.d(TAG, "InitListener init() code = " + code);
            if (code != ErrorCode.SUCCESS) {
                Log.d(TAG, "初始化失败");
            }
        }
    };
    private void initImageLoader() {
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
//                .showImageOnLoading(R.drawable.ic_empty)
//                .showImageForEmptyUri(R.drawable.ic_empty)
//                .showImageOnFail(R.drawable.ic_error)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.ARGB_8888)
                .build();

        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(this);
        config.defaultDisplayImageOptions(defaultOptions);
        config.threadPriority(Thread.NORM_PRIORITY - 2);
        // 并发
        config.threadPoolSize(100);
        config.denyCacheImageMultipleSizesInMemory();
        config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
        config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
        config.tasksProcessingOrder(QueueProcessingType.LIFO);

        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config.build());
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

    public void setSettingSexChoose(String paramBoolean) {
        editor.putString(SettingSexChoose, paramBoolean);
        editor.commit();
    }
    public boolean getSettingAutoPlayer() {
        return mSharedPreferences.getBoolean(AUTOPLAYER, true);
    }

    public String getSettingSexChoose() {
        return mSharedPreferences.getString(SettingSexChoose, "f");
    }

    public void setSettingSpeedChoose(float paramBoolean) {
        editor.putFloat(SettingSpeedChoose, paramBoolean);
        editor.commit();
    }
    public boolean getSettingMsgSpeaker(){
        return mSharedPreferences.getBoolean(SettingMsgSpeaker,false);
    }
    public void  setSettingMsgSpeaker(boolean paramBoolean){
        editor.putBoolean(SettingMsgSpeaker, paramBoolean);
        editor.commit();
    }

    public float getSettingSpeedChoose() {
        return mSharedPreferences.getFloat(SettingSpeedChoose, 1f);
    }

    public void setSettingAutoPlayer(boolean paramBoolean) {
        editor.putBoolean(AUTOPLAYER, paramBoolean);
        editor.commit();
    }
    public static synchronized DatabaseHelper getHelper(Context context) {
        if (dbinstance == null) {
            synchronized (DatabaseHelper.class) {
                if (dbinstance == null)
                    dbinstance = new DatabaseHelper(context);
            }
        }
        return dbinstance;
    }

}
